<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>NewBee商城-登录</title>
    <link rel="stylesheet" th:href="@{mall/css/common.css}">
    <link rel="stylesheet" th:href="@{mall/styles/login.css}">
    <link rel="stylesheet" th:href="@{mall/styles/header.css}">
    <link rel="stylesheet" th:href="@{mall/css/captcha.css}"/>
    <link rel="stylesheet" th:href="@{/admin/plugins/sweetalert/sweetalert.css}"/>
    <script th:inline="javascript">var _ctx = [[@{/}]];</script>
</head>
<body>
<div class="top center">
    <div class="logo center">
        <a href="./index" target="_blank"><img src="mall/image/login-logo-2.png" alt=""></a>
    </div>
</div>
<div class="form center">
    <div class="login">
        <div class="login_center">
            <div class="login_top">
                <div class="left fl">会员登录</div>
                <div class="right fr">您还不是我们的会员？<a th:href="@{/register}" target="_self">立即注册</a></div>
                <div class="clear"></div>
                <div class="under-line center"></div>
            </div>
            <form id="loginForm" onsubmit="return false;" action="##">
                <div class="login_main center">
                    <div class="login-info">手机号:&nbsp;<input class="login-info-input" type="text" name="loginName"
                                                             id="loginName"
                                                             placeholder="请输入你的手机号"/>
                    </div>
                    <div class="login-info">密&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;<input class="login-info-input"
                                                                                    id="password"
                                                                                    type="password"
                                                                                    name="password"
                                                                                    placeholder="请输入你的密码"/></div>
                    <!--                    <div class="login-info">-->
                    <!--                        验证码:&nbsp;-->
                    <!--                        <input class="login-info-input verify-code" type="text" name="verifyCode"-->
                    <!--                               placeholder="请输入验证码" id="verifyCode"/>-->
                    <!--                        <img alt="单击图片刷新！" style="top: 14px;position: relative;" th:src="@{/common/captcha}"-->
                    <!--                             th:onclick="this.src=_ctx + 'common/captcha?d='+new Date()*1">-->
                    <!--                    </div>-->
                </div>
                <div class="login_submit">
                    <input class="submit" type="submit" onclick="loginCheck()" value="立即登录">
                </div>
            </form>
        </div>
    </div>
</div>
<div class="captcha-modal">
    <div class="slider">
        <div class="content">
            <div class="bg-img-div">
                <img id="bg-img" src="" alt/>
            </div>
            <div class="slider-img-div" id="slider-img-div">
                <img id="slider-img" src="" alt/>
            </div>
        </div>
        <div class="slider-move">
            <div class="slider-move-track">
                拖动滑块完成拼图
            </div>
            <div class="slider-move-btn" id="slider-move-btn"></div>
        </div>
        <div class="bottom">
            <div class="close-btn" id="slider-close-btn"></div>
            <div class="refresh-btn" id="slider-refresh-btn"></div>
        </div>
    </div>
</div>
</body>
<!-- jQuery -->
<script th:src="@{/admin/plugins/jquery/jquery.min.js}"></script>
<script th:src="@{/admin/dist/js/public.js}"></script>
<script th:src="@{/admin/plugins/sweetalert/sweetalert.min.js}"></script>
<script th:src="@{/mall/js/tianai/captcha.js}"></script>
<script type="text/javascript">

    let currentCaptchaId = null;
    $(function () {
        clearAllPreventDefault($(".slider"));
        refreshCaptcha();
    })

    $("#slider-move-btn").mousedown(down);
    $("#slider-move-btn").on("touchstart", down);

    $("#slider-close-btn").click(() => {
        hideCaptcha();
    });

    $("#slider-refresh-btn").click(() => {
        refreshCaptcha();
    });

    function valid(captchaConfig) {
        let that = this;
        let data = {
            bgImageWidth: captchaConfig.bgImageWidth,
            bgImageHeight: captchaConfig.bgImageHeight,
            templateImageWidth: captchaConfig.templateImageWidth,
            templateImageHeight: captchaConfig.templateImageHeight,
            startSlidingTime: captchaConfig.startTime,
            entSlidingTime: captchaConfig.stopTime,
            trackList: captchaConfig.trackArr
        };
        console.log("valid", captchaConfig, data);
        $.ajax({
            type: "POST",
            url: _ctx + "tianai/check?id=" + currentCaptchaId,
            contentType: "application/json", //必须这样写
            dataType: "json",
            data: JSON.stringify(data),//schoolList是你要提交是json字符串
            success: function (res) {
                console.log(res);
                if (res) {
                    that.login();
                } else {
                    refreshCaptcha();
                }
            }

        })
    }

    function refreshCaptcha() {
        $.get(_ctx + "tianai/gen", function (data) {
            reset();
            currentCaptchaId = data.id;
            const bgImg = $("#bg-img");
            const sliderImg = $("#slider-img");
            bgImg.attr("src", data.captcha.backgroundImage);
            sliderImg.attr("src", data.captcha.templateImage);
            initConfig($(".bg-img-div").width(), bgImg.height(), sliderImg.width(), sliderImg.height(), 206);
        })
    }

    function doDown() {
        $("#slider-move-btn").css("background-position", "-5px 31.0092%")
    }


    function doMove(currentCaptchaConfig) {
        const moveX = currentCaptchaConfig.moveX;
        $("#slider-move-btn").css("transform", "translate(" + moveX + "px, 0px)")
        $("#slider-img-div").css("transform", "translate(" + moveX + "px, 0px)")
    }

    function reset() {
        $("#slider-move-btn").css("background-position", "-5px 11.79625%")
        $("#slider-move-btn").css("transform", "translate(0px, 0px)")
        $("#slider-img-div").css("transform", "translate(0px, 0px)")
        currentCaptchaId = null;
    }

    function showCaptcha() {
        $(".captcha-modal").show();
    }

    function hideCaptcha() {
        refreshCaptcha();
        $(".captcha-modal").hide();
    }

    function loginCheck() {
        var loginName = $("#loginName").val();
        if (!validPhoneNumber(loginName)) {
            swal('请输入正确的登录名(即手机号)', {
                icon: "error",
            });
            return false;
        }
        var password = $("#password").val();
        if (!validPassword(password)) {
            swal('请输入正确的密码格式(6-20位字符和数字组合)', {
                icon: "error",
            });
            return false;
        }
        showCaptcha();
    }

    function login() {
        var url = _ctx + 'login?destPath=';
        // 添加目标地址
        var destPath = window.location.href.split('?')[1];
        if (destPath) {
            url = url + destPath;
        }
        // 验证
        var params = $("#loginForm").serialize();
        $.ajax({
            type: 'POST',//方法类型
            url: url,
            data: params,
            success: function (result) {
                if (result.code == 200) {
                    if (result.map.destPath) {
                        window.location.href = _ctx + result.map.destPath;
                    } else {
                        window.location.href = _ctx + 'index';
                    }
                } else {
                    swal(result.msg, {
                        icon: "error",
                    });
                    hideCaptcha();
                }
            },
            error: function () {
                swal("操作失败", {
                    icon: "error",
                });
            }
        });
    }
</script>
</html>
